﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using KpBusiness.Model;
using KpBusiness.Layer;
using NHibernate;

namespace KpBusiness.Service
{
    public class PositionService
    {
        ///get all positions
        ///get position
        ///create/save position
        
        private static readonly ILog log = LogManager.GetLogger("Services");

        /// <summary>
        /// returns all positions
        /// </summary>
        /// <returns>returns list of positioins</returns>
        public IList<Position> GetAllPositions()
        {

            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            IList<Position> students = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();

                ICriteria criteria = session.CreateCriteria(typeof(Position));
                students = criteria.List<Position>();
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return students;
        }

        /// <summary>
        /// returns single position
        /// </summary>
        /// <param name="positionId"></param>
        /// <returns></returns>
       public Position GetPosition(int positionId)
        {
            log.Debug("Started");
            ISession session = DataAccessLayerHelper.OpenReaderSession();
            Position position = null;
            ITransaction transaction = null;
            try
            {
                transaction = session.BeginTransaction();
                position = session.Get<Position>(positionId);
                session.Flush();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                if (transaction != null && transaction.IsActive)
                    transaction.Rollback();
                log.Error(ex);

            }
            finally
            {
                if (transaction != null)
                    transaction.Dispose();

                if (session != null && session.IsConnected)
                    session.Close();
            }

            log.Debug("End");
            return position;
        }

        /// <summary>
        /// save position
        /// </summary>
        /// <param name="position"></param>
       public void SavePosition(Position position)
       {
           log.Debug("Started");
           ISession session = DataAccessLayerHelper.OpenWriterSession();

           ITransaction transaction = null;
           try
           {
               transaction = session.BeginTransaction();
               session.SaveOrUpdate(position);
               session.Flush();
               transaction.Commit();
           }
           catch (Exception ex)
           {
               if (transaction != null && transaction.IsActive)
                   transaction.Rollback();
               log.Error(ex);

           }
           finally
           {
               if (transaction != null)
                   transaction.Dispose();

               if (session != null && session.IsConnected)
                   session.Close();
           }

           log.Debug("End");

       }
    }
}
